package javabase.demo03;


public class SynchronizedPractice {
    private static int count = 0;
    private static final Object lock = new Object();

    public static void main(String[] args) throws InterruptedException {

        // 1. 同步代码块
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 10000; i++) {
                synchronized (lock) { // 同步代码块
                    count++;
                }
            }
        });

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 10000; i++) {
                synchronized (lock) { // 同步代码块
                    count++;
                }
            }
        });

        t1.start();
        t2.start();
        t1.join();
        t2.join();

        System.out.println("同步后计数: " + count);
        // 2. 同步方法
        Counter counter = new Counter();
        Thread t3 = new Thread(() -> {
            for (int i = 0; i < 10000; i++) {
                counter.increment();
            }
        });

        Thread t4 = new Thread(() -> {
            for (int i = 0; i < 10000; i++) {
                counter.increment();
            }
        });

        t3.start();
        t4.start();
        t3.join();
        t4.join();

        System.out.println("同步方法计数: " + counter.getCount());
        // 3. 静态同步方法
        demonstrateStaticSynchronized();
        // 4. 锁升级过程演示（需要通过JVM参数观察）
        demonstrateLockUpgrade();
    }

    // 静态同步方法
    private static void demonstrateStaticSynchronized() throws InterruptedException {
        System.out.println("\n=== 静态同步方法演示 ===");

        count = 0;

        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 10000; i++) {
                incrementStatic();
            }
        });

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 10000; i++) {
                incrementStatic();
            }
        });

        t1.start();
        t2.start();
        t1.join();
        t2.join();

        System.out.println("静态同步方法计数: " + count);
    }

    // 静态同步方法
    private static synchronized void incrementStatic() {
        count++;
    }

    // 锁升级过程演示（需要通过JVM参数观察）
    private static void demonstrateLockUpgrade() {
        System.out.println("\n=== 锁升级过程演示 ===");
        System.out.println("请使用JVM参数运行以观察锁升级: -XX:+PrintFlagsFinal");
        System.out.println("偏向锁延迟: -XX:BiasedLockingStartupDelay=0");
        System.out.println("禁用偏向锁: -XX:-UseBiasedLocking");

        Object obj = new Object();

        // 初始状态：无锁
        System.out.println("初始状态: 无锁");

        // 第一次加锁：偏向锁
        synchronized (obj) {
            System.out.println("第一次加锁: 偏向锁");
        }

        // 有竞争时：轻量级锁
        Thread t1 = new Thread(() -> {
            synchronized (obj) {
                System.out.println("线程1加锁: 轻量级锁");
            }
        });

        Thread t2 = new Thread(() -> {
            synchronized (obj) {
                System.out.println("线程2加锁: 轻量级锁");
            }
        });

        t1.start();
        t2.start();

        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // 重度竞争时：重量级锁
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                synchronized (obj) {
                    try {
                        Thread.sleep(100); // 模拟耗时操作
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }

        System.out.println("重度竞争: 重量级锁");
    }
}
// 同步方法示例
class Counter {
    private int count = 0;

    // 同步方法
    public synchronized void increment() {
        count++;
    }

    public synchronized int getCount() {
        return count;
    }
}